﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XmlManager;
using Mirror.Models;
using Mirror.Strategies;
using MQL;
using System.Threading;

namespace Mirror
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Initializing accounts...");

            List<Account> accounts = XmlManager.XmlManager.Deserialize<List<Account>>("accounts.xml");

            foreach (var account in accounts)
            {
                if (!account.Connect())
                {
                    System.Console.WriteLine("Account didn't connect: " + account.Broker + " " + account.Login);
                    Environment.Exit(1);
                }
                else
                {
                    System.Console.WriteLine("Account {0}/{1} connected. ", account.Broker, account.Login);
                }
            }

            System.Console.WriteLine("Connected to all accounts.");

            System.Console.WriteLine("Reading settings...");

            
            List<StrategySettings> settings = XmlManager.XmlManager.Deserialize<List<StrategySettings>>("settings.xml");

            if (settings.Count == 0)
            {
                System.Console.WriteLine("Settings not specified correctly.");
                Environment.Exit(1);
            }

            List<Strategy> strategies = new List<Strategy>();

            foreach (var s in settings)
            {
                strategies.Add(Strategy.GenerateStrategy(s));
            }

            System.Console.WriteLine("Settings loaded.");


            while (true)
            {
                // TODO: Logger
                System.Console.WriteLine("Starting strategy {0}...", strategies[0].Name);
                Thread.Sleep(5000);
                double TargetVolume;
                Strategy strategy = strategies[0];
                if (strategy.TryGetTargetVolume(out TargetVolume))
                {
                    Account account = accounts.Find(acc => acc.Login == strategy.Login && acc.Broker == strategy.Broker);
                    if (account == null)
                    {
                        // TODO: Logger
                        System.Console.WriteLine("Strategy has a wrong account assigned.");
                    }
                    else
                    {
                        OrdersHelper.OrderResult result = account.OrdersHelper.SendOrder(TargetVolume, strategy.VolumeDifference, strategy.Symbol, strategy.MagicNumber, strategy.MinLotSize);
                        // TODO: Logger
                        System.Console.WriteLine(result);
                    }
                }


            }

            // TODO:
            // 1) Logger - przygotować klase z jedną instancją do tworzenia logów
            // 2) ProvidersDownloader - dodać odświeżanie danych - czyli pobierać co ileś tam czasu dane z baz danych odświeżone
            // 3) Utworzenie oddzielnych wątków dla każdej strategii - dodatkowo obsłużyć (byle jak - byle nie wyjebywało apki) wyjątki. 
            //



        }
    }
}

